<template slot-scope="scope">
  <div>
    <el-checkbox :indeterminate="isIndeterminateDiscountMode" v-model="checkAllDiscountMode" @change="handleRepairCheckAllChange">优惠模式</el-checkbox>
    <div style="margin:0px 0px 0px 20px;width:20px;">
      <el-checkbox-group v-model="checkedDiscountModeList" @change="handleCheckedCitiesChangeDiscountMode">
        <el-checkbox v-for="discountMode in sitesDiscountMode" :label="discountMode.DISCOUNT_MODE_CODE" :key="discountMode.DISCOUNT_MODE_CODE" :value="discountMode.DISCOUNT_MODE_CODE">
          {{discountMode.DISCOUNT_MODE_NAME}}
        </el-checkbox>
      </el-checkbox-group>
    </div>
  </div>
</template>


<script>
  import { mapState } from 'vuex';
  import { dictionary } from '@/utils/dictMixin';
  import { getDiscountMode,getQueryDiscountMode } from '@/api/common/auth/roleManage'
  export default {
    name: "favorableModels",
    props:['userPermissionData'],
    mixins: [dictionary],
    data() {
      return {
        checkAllDiscountMode: false,
        checkedDiscountModeList: [],
        loading: false,
        isIndeterminateDiscountMode: false,
        sitesDiscountMode: [],
        roleId: '',
      }
    },
    created() {
      this.loadData()
    },
    methods: {
      async loadData() {
        this.loading = true;
        const queryDiscountModeDate = await getQueryDiscountMode()
        console.log(queryDiscountModeDate,"优惠模式1")
        this.loading = false
        // console.log(queryDiscountModeDate.data,"queryDiscountModeDate.data")
        if(queryDiscountModeDate.data && queryDiscountModeDate.data.length>0) {
          this.sitesDiscountMode = _.cloneDeep(queryDiscountModeDate.data);
        }

        console.log(this.userPermissionData,"优惠模式")
        if(this.userPermissionData.favorableModels){
          let checkList = this.userPermissionData.favorableModels.split(",")
          checkList.forEach(x=>{
            this.checkedDiscountModeList.push(x)
          })
          if(this.checkedDiscountModeList.length == this.sitesDiscountMode.length){
            this.checkAllDiscountMode=true
          }
        }
      },
      handleRepairCheckAllChange(val) {
        const checkedDiscountModeList = [];
        if (val) {
          for (var i = 0; i < this.sitesDiscountMode.length; i++) {
            checkedDiscountModeList.push(this.sitesDiscountMode[i].DISCOUNT_MODE_CODE);
          }
        }
        this.checkedDiscountModeList = checkedDiscountModeList;
        this.isIndeterminateDiscountMode = false;
      },
      handleCheckedCitiesChangeDiscountMode(value) {
        let checkedCount = value.length;
        this.checkAllDiscountMode = checkedCount === this.sitesDiscountMode.length;
        this.isIndeterminateDiscountMode = checkedCount > 0 && checkedCount < this.sitesDiscountMode.length;
      },
      getData() {
        return [...this.checkedDiscountModeList]
      }
    }
  }

</script>

<style scoped>
  .el-checkbox+.el-checkbox {
    margin-left: 0px;
  }
  .el-checkbox {
    margin-right: 25px;
  }

</style>
